<?php
//Zend_Loader::loadFile('../application/admin/models/Template.php');
class Campaign
{
    /**
     * @var object Zend DB Adapter
     */
    private $_db;
    /**
     * @var string
     */
   
    public function __construct()
    {
			$this->_db = Zend_Registry::get('db');
    }
    
    public function listCampaigns()
    {
    	$sql="select c.id,c.title,c.description,c.active,c.nationalpark,c.title_ru,c.description_ru,a.title as park from campaign c inner join about_country a on  c.nationalpark=a.page_id  ";
 //	echo $sql;exit;
    	$getCampaigns = $this->_db->fetchAll($sql);
		
		return $getCampaigns;
	}
	
	public function getCampaignbyid($id)
    {
    	$sql="select * from campaign where  id=".$id."	";
    	//echo $sql;exi
    	$getCampaign = $this->_db->fetchrow($sql);
		
		return $getCampaign;
	}
	public function updateCampaign($val)
        {
            
            $sql="update  campaign set  title='".$val["title"]."',title_ru='".$val["title_ru"]."',description='".$val["description"]."',description_ru='".$val["description_ru"]."',active=".$val["active"].",nationalpark='".$val["park_id"]."' where  id=".$val["id"]."	";
             // echo $sql; exit;
            $updateCampaign=$this->_db->exec($sql);

        	echo "succsfully updated";
       
            
        }

        public function addCampaign($val)
        {

            $sql="insert into  campaign (title,description,title_ru,description_ru,active,nationalpark) values ('".$val["title"]."','".$val["description"]."','".$val["title_ru"]."','".$val["description_ru"]."'," .$val["active"]."," .$val["park_id"]." )";
            //echo $sql;
            $updateCampaign=$this->_db->exec($sql);

        	echo "succsfully updated";


        }

    public function getHotel($hotel_id)
    {
    	$sql="
			select hotel_id,hotel_title,hotel_city,hotel_country,hotel_description,
    	 hotel_contactperson,hotel_phone,hotel_fax,hotel_skype,is_Enabled
			from  Rates where   hotel_id='".$hotel_id."'
			
		";  
    	$getHotel = $this->_db->fetchrow($sql);	
		return $getHotel;       
    }

    public function addRate($form_values)
    {
    	$hotel_id = $form_values['hotel_id'];
    	$rate_category = $form_values['rate_category'];
    	$rate_basis = $form_values['rate_basis'];
    	$fromdate = $form_values['fromdate'];
    	$todate = $form_values['todate'];
    	$rate_additionalinfo = $form_values['rate_additionalinfo'];
    	
    	$sql="INSERT INTO hotel_rates(hotel_id, rate_category, rate_basis, fromdate, todate, rate_additionalinfo)
    		  VALUES(" . $hotel_id . ", '" . $rate_category . "', '" . $rate_basis . "', '" . $fromdate . "', '" . $todate . "', '".$rate_additionalinfo."')";
    	 //echo $sql;exit;
    	$addrate=$this->_db->exec($sql);
        if($addrate)
        {
        	echo "A new record has been succsfully created";
        }
    } 
    
    public function updateRate($form_values)
    {
    	$hotel_id = $form_values['hotel_id'];
        $rate_id=$form_values['rate_id'];
    	$rate_category = $form_values['rate_category'];
    	$rate_basis = $form_values['rate_basis'];
    	$fromdate = $form_values['fromdate'];
    	$todate = $form_values['todate'];
    	$rate_additionalinfo = $form_values['rate_additionalinfo'];
    	
    	$sql = "UPDATE hotel_rates 
    			SET rate_category='".$rate_category."', rate_basis='".$rate_basis."',
    				fromdate='".$fromdate."', todate='".$todate."',
    				rate_additionalinfo='".$rate_additionalinfo."' 
    			WHERE rate_id=".$rate_id;
    	// echo $sql;exit;
        $updaterates = $this->_db->exec($sql);        
        if($updaterates)
        {
        	echo "Record has been successfully updated";
        }
    }   

    public function deleteCampaign($id)
    {
    	$sql = "DELETE FROM campaign
    			WHERE id = " . $id;
    	
   	//echo $sql;exi;
        $delrates = $this->_db->exec($sql);

      		echo "Campaign has been successfully deleted";
      	 
    	//echo "Selected rate has been successfully deleted";
    }

    public function updateCampaignfeatures($request)
    {
            $dberror=0;
            //  print_r($request);
             $insertstructarray=array();
             foreach($request["key"] as $key=>$val)
             {

                      $insertstructarray[$val]["val"]=$request["val"][$key];
                       $insertstructarray[$val]["val_ru"]=$request["val_ru"][$key];

             }
             //  print_r($insertstructarray);exit;
                $this->_db->exec("SET AUTOCOMMIT=0");
                $this->_db->exec("START TRANSACTION");
                        $deletesql="delete from  campaigndetails  where Campaignid =".$request["id"]."";
		         $sqldelete =  $this->_db->exec($deletesql);
                    //   echo $deletesql;exit;
                foreach($insertstructarray as $structurekey=>$structurevalue){
                    if(strlen($structurekey)>0){
                        $sql1="insert  into  campaigndetails (campaignid,ckey,val,val_ru) values ('".$request["id"]."','".$structurekey."','".$structurevalue["val"]."','".$structurevalue["val_ru"]."')";
                        // echo $sql1;exit;
                        $sqlinsert=  $this->_db->exec($sql1);
                     
                        if(mysql_error())
                        {
                            $dberror=1;
                        }
                      
                    }
                }

                if($dberror==0)
                {
                    $this->_db->exec("COMMIT");
                     echo "Campaign details has been successfully updated";
                }
                else
                {
                    $this->_db->exec("ROLLBACK");
                    echo "Update failed.Database error occured!";
                }


               // exit;
		//return $success;
    }

    public function getCampaignfeatures($id)
    {

        $sql="select * from campaigndetails where Campaignid=".$id."	";
     //	 echo $sql;exit;
    	$getCampaignfeatures = $this->_db->fetchAll($sql);
        //print_r($getCampaignfeatures);exit;
        return $getCampaignfeatures;
        
    }
     public function getnationalparks()
    {

        $sql="select page_id,title from about_country  where category='National Parks'";
      //	 echo $sql;exit;
    	$getnationalparks = $this->_db->fetchAll($sql);
        //print_r($getCampaignfeatures);exit;
        return $getnationalparks;

    }
    public function deleteCampaignfeatures($arr)
    {
        $sql="delete  from campaigndetails where id=".$arr["id"]." "	;
    	$deleteCampaigndetail = $this->_db->exec($sql);
        if($deleteCampaigndetail)
        {
            echo "Successfully deleted!";
        }
    }
}